package Database;

import Entity.Admin;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CONG
 */
public class AdminDB {
    private ConnectDB conn;

    public AdminDB() {
        conn=new ConnectDB();
    }
    
    public ArrayList<Admin> getAdminList(){
        ArrayList<Admin> listAdmin=new ArrayList<Admin>();
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call getAdminList}");
            ResultSet rs=cstmt.executeQuery();
            while(rs.next()){
                
                int id=rs.getInt("AdminID");
                String name=rs.getString("AdminAccount");
                String adminPass=rs.getString("AdminPass");
                String fullName=rs.getString("FullName");
                String address=rs.getString("Address");
                String phone=rs.getString("Phone");
                String email=rs.getString("Email");
                int roleCode=rs.getInt("RoleCode");
                Admin ad=new Admin(id, name, adminPass, fullName, address, phone, email, roleCode);
                listAdmin.add(ad);
            }
        } catch (Exception ex) {
            Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listAdmin;
    }
    public Admin insertAdmin(Admin ad){
        try {
           CallableStatement cstmt=conn.connect().prepareCall("{call insertAdmin(?,?,?,?,?,?,?,?)}");
           cstmt.registerOutParameter("AdminID", Types.INTEGER);
           cstmt.setString("AdminAccount", ad.getAdminAccount());
           cstmt.setString("AdminPass",ad.getAdminPass());
           cstmt.setString("FullName", ad.getFullName());
           cstmt.setString("Address", ad.getAddress());
           cstmt.setString("Phone",ad.getPhone());
           cstmt.setString("Email", ad.getEmail());
           cstmt.setInt("RoleCode", ad.getRoleCode());
           cstmt.executeUpdate();
           int adminID=cstmt.getInt("AdminID");
           ad.setAdminID(adminID);
        } catch (Exception ex) {
            Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ad;
    }
    public void deleteAdmin(Admin ad){
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call deleteAdmin(?)}");
            cstmt.setInt("AdminID", ad.getAdminID());
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public Admin checkLoginAdmin(String name,String pass){
       Admin admin=null;
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call checkLoginAdmin(?,?)}");
            cstmt.setString("adminAccount", name);
            cstmt.setString("adminPass", pass);
            ResultSet rs=cstmt.executeQuery();
           while(rs.next()){
              int adminID=rs.getInt("AdminID");
              String adminAccount=rs.getString("AdminAccount");
              //System.out.println(adminAccount);
              String passWord=rs.getString("AdminPass");
              String fullName=rs.getString("FullName");
              String address=rs.getString("Address");
              String phone=rs.getString("Phone");
              String email=rs.getString("Email");
              int roleCode=rs.getInt("RoleCode");
              admin=new Admin(adminID, adminAccount, passWord, fullName, address, phone, email, roleCode);
           }
        } catch (Exception ex) {
            Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return admin;
    }
    public void updateProfile(Admin ad){
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call editProfile(?,?,?,?,?)}");
            cstmt.setInt("adminID", ad.getAdminID());
            cstmt.setString("fullName", ad.getFullName());
            cstmt.setString("address", ad.getAddress());
            cstmt.setString("phone", ad.getPhone());
            cstmt.setString("email", ad.getEmail());
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void changePassword(Admin ad){
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call changePassword(?,?)}");
            cstmt.setInt("adminID", ad.getAdminID());
            cstmt.setString("pass", ad.getAdminPass());
            cstmt.executeUpdate();
        } catch (Exception ex) {
             Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public boolean checkEmailAdmin(String email){
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call checkEmailAdmin(?)}");
            cstmt.setString("email", email);
            ResultSet rs=cstmt.executeQuery();
            return rs.next();
        } catch (Exception ex) {
            Logger.getLogger(AdminDB.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
